package com.itheima.health.h5.controller;

import com.itheima.health.dto.SetmealDTO;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.service.CheckGroupService;
import com.itheima.health.service.SetmealService;
import com.itheima.health.utils.aliyunoss.AliyunUtils;
import com.itheima.health.utils.redis.RedisUtil;
import com.itheima.health.utils.resources.RedisConstant;
import com.itheima.health.utils.resources.UploadUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;


@RestController
@Api(tags = "检查项模块管理")
public class SetmealController {
    @Reference
    private SetmealService setmealService;
    @Reference
    private CheckGroupService checkGroupService;

    //   开发 检查项模块   crud


    @GetMapping("setmeal/findAll")
    @ApiOperation(value = "查询检查项方法", notes = "查询所有的检查项列表信息")
    public Result findAll() {
        List<CheckGroup> list = checkGroupService.list();
        return new Result(list);
    }

  @PostMapping("setmeal/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
      PageResult page = setmealService.findPage(queryPageBean);
      return new Result(page);
    }

    @PostMapping("setmeal/saveSetemal")
    public Result add(@RequestBody SetmealDTO setmealDTO) {
        Boolean flag = setmealService.saveSetemal(setmealDTO);

        return new Result(flag);
    }

    @RequestMapping("setmeal/upload")
    public Result upload (@RequestParam("imgFile") MultipartFile file) {
        try {
            String originalFilename = file.getOriginalFilename();
            String uuidFilename = UploadUtils.generateRandonFileName(originalFilename);
            AliyunUtils.uploadMultiPartFileToAliyun(file.getBytes(),uuidFilename);
            // 上传好阿里云图片之后 我们将图片存储到redis set 集合中
            RedisUtil.addToSet(RedisConstant.ALL_SETMEAL_PIC_SET,uuidFilename);
            //  将上传到阿里云的图片文件 保存在redis中
            RedisUtil.set(RedisConstant.SINGLE_PIC+uuidFilename,uuidFilename,1, TimeUnit.MINUTES);
            return new Result(uuidFilename);//  一定要把阿里云上的文件名  回送给客户端 ，因为添加套餐时 需要存储到数据库的图片路径
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }

    }

     /* @GetMapping("checkGroup/findCheckItemIdsbyGroupId/{id}")
    public Result deleteCheckItemById(@PathVariable("id") int id){
        CheckGroupVO checkGroupVO = checkGroupService.findCheckItemIdsbyGroupId(id);

        return new Result(checkGroupVO);
    }*/
}

